package com.happy.cat.project.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happy.cat.project.system.domain.CatTeamCategoryItem;
import com.happy.cat.project.system.domain.CatTeamCategoryTag;
import com.happy.cat.project.system.domain.UserAccount;
import com.happy.cat.project.system.domain.UserManitoApply;
import com.happy.cat.project.system.domain.vo.UserManitoVo;
import com.happy.cat.project.system.mapper.UserManitoApplyMapper;
import com.happy.cat.project.system.service.ICatTeamCategoryItemService;
import com.happy.cat.project.system.service.ICatTeamCategoryTagService;
import com.happy.cat.project.system.service.IUserAccountService;
import com.happy.cat.project.system.service.IUserManitoApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 大神申请审核Service业务层处理
 *
 * @author verne
 * @date 2020-08-13
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserManitoApplyServiceImpl extends ServiceImpl<UserManitoApplyMapper, UserManitoApply> implements IUserManitoApplyService {

    @Autowired
    public ICatTeamCategoryItemService teamCategoryItemService;

    @Autowired
    public IUserAccountService userAccountService;

    @Autowired
    public ICatTeamCategoryTagService teamCategoryTagService;

    @Override
    public List<UserManitoVo> listVoItems(List<UserManitoApply> list) {

        Set<Long> uids = new HashSet<>();
        Set<Long> cids = new HashSet<>();
        Set<Long> tids = new HashSet<>();

        list.stream().forEach(e -> {
            uids.add(e.getAccountId());
            cids.add(e.getTeamCategoryItemId());
            tids.add(e.getRegionId());
            tids.add(e.getHighestStrengthId());
        });
        Map<Long, UserAccount> userMap = userAccountService.listByIds(uids).stream().collect(Collectors.toMap(UserAccount::getId, e -> e));
        Map<Long, CatTeamCategoryItem> gategoryMap = teamCategoryItemService.listByIds(cids).stream().collect(Collectors.toMap(CatTeamCategoryItem::getId, e -> e));
        Map<Long, CatTeamCategoryTag> tagMap = teamCategoryTagService.listByIds(cids).stream().collect(Collectors.toMap(CatTeamCategoryTag::getId, e -> e));

        List<UserManitoVo> vos = new ArrayList<>();
        list.forEach(e -> {
            UserManitoVo vo = e.convertToVo();
            if(userMap.get(e.getAccountId())!=null){
                vo.setUserNike(userMap.get(e.getAccountId()).getNick());
            }
            if(gategoryMap.get(e.getTeamCategoryItemId())!=null){
                vo.setGategoryName(gategoryMap.get(e.getTeamCategoryItemId()).getName());
                vo.setUnit(gategoryMap.get(e.getTeamCategoryItemId()).getTollType());
            }
            if(tagMap.get(e.getRegionId())!=null){
                vo.setRegionName(tagMap.get(e.getRegionId()).getName());
            }
            if(tagMap.get(e.getHighestStrengthId())!=null){
                vo.setHighestStrengthName(tagMap.get(e.getHighestStrengthId()).getName());
            }
            vos.add(vo);
        });
        return vos;
    }
}
